
export const getToken = () => sessionStorage.getItem('token') || ''

export const setToken = token => {
  sessionStorage.setItem('token', token)
}

export const setEmail = email => {
  sessionStorage.setItem('email', email)
}

export const getEmail = () => sessionStorage.getItem('email') || ''

export const join = (value, separator = '') => {
  if(!value && !Array.isArray(value)) {
    return ''
  } else {
    return value.join(separator)
  }
}




/**
 * @param {Number} num 数值
 * @returns {String} 处理后的字符串
 * @description 如果传入的数值小于10，即位数只有1位，则在前面补充0
 */
const getHandledValue = num => {
  return num < 10 ? '0' + num : num
}

/**
 * 日期格式化
 * @param { Date | String | Number } date 日期
 */
export const formatDate = (date, format) => {
  const d = new Date(date)
  if(d == 'Invalid Date' || date === null) {
    return '-'
  }
  const year = d.getFullYear()
  const month = getHandledValue(d.getMonth() + 1)
  const day = getHandledValue(d.getDate())
  const hours = getHandledValue(d.getHours())
  const minutes = getHandledValue(d.getMinutes())
  // const second = getHandledValue(d.getSeconds())
  if(format === 'yyyy-MM-dd') {
    return `${year}年${month}月${day}日`
  } else if (format === 'yyyy-MM') {
    return `${year}年${month}月`
  }
  return `${year}年${month}月${day}日 ${hours}点${minutes}分`
}


/**
 * @param { number } status 0 已发表
 * @param { number } status 1 已上传未发表
 * @param { number } status 2 已撤回
 */
export const STATUS = {
  RELEASED: 0,
  TO_BE_RELEASED: 1,
  WITHDRAWN: 0
}


/**
 * @description 防抖函数
 * @param { function } fn 需要执行的函数
 * @param { number } interval 防抖间隔
 */
export const debounce = (fn, interval = 300) => {
  let timeout = null;
  return function () {
      clearTimeout(timeout);
      timeout = setTimeout(() => {
          fn.apply(this, arguments);
      }, interval);
  };
}

/**
 * @description 生成uid
 */
export const guid = () => {
  return 'xxxxx4xyxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
  });
}